﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;


public class SoldierVehicle : BattleComponent<SoldierVehicle> {
    public Ticker VehicleTicker = new Ticker(1,true);

    public NavMeshAgent navMeshAgent;
    public SoldierAI soldierAI;

    public Vector3 target;
    public Quaternion targetRotation;

    public float runSpeed;
    public float walkSpeed;

    //移动的单位要避让静止的单位
    private const int idleColllisionPriority = 49;
    private const int moveColllisionPriority = 50;

    public float movingRadius;
    public float fightingRadius;


    private void OnEnable()
    {
        Assign(this);
        navMeshAgent.enabled = true;
    }

    private void OnDisable()
    {
        Unassign(this);
        navMeshAgent.enabled = false;
        
    }

    private void Start()
    {
        navMeshAgent.Warp(transform.position);
    }

    public void SetUpNavMeshAgent()
    {
        if (target == Vector3.zero) return;
        switch (soldierAI.currentState)
        {
            case SoldierState.Idle:
                navMeshAgent.isStopped = false;
                navMeshAgent.avoidancePriority = idleColllisionPriority;
                transform.rotation = targetRotation;
                break;
            case SoldierState.MeleeAttacking:
            case SoldierState.RangedAttacking:
            case SoldierState.Locking:
                navMeshAgent.isStopped = true;
                navMeshAgent.radius = fightingRadius;
                break;
            case SoldierState.Moving:
                navMeshAgent.isStopped = false;
                navMeshAgent.avoidancePriority = moveColllisionPriority;
                navMeshAgent.speed = runSpeed;
                navMeshAgent.radius = movingRadius;
                break;
            case SoldierState.Preparing:
                navMeshAgent.isStopped = false;
                navMeshAgent.speed = walkSpeed;
                navMeshAgent.radius = fightingRadius;
                break;
            case SoldierState.Charging:
                navMeshAgent.isStopped = false;
                navMeshAgent.avoidancePriority = 1;
                navMeshAgent.speed = runSpeed;
                navMeshAgent.radius = movingRadius;
                break;
        }
        navMeshAgent.SetDestination(target);
    }

    public static void UpdateNavMeshAgent()
    {
        foreach(var v in ComponentDataList)
        {
            if(v.VehicleTicker.Tick()) v.SetUpNavMeshAgent();
        }
    }




    

    

}
